﻿using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms.Impl;


public class ballups : MonoBehaviour {
	public int veloCity = 1;// 控制物体上升的速度
    public bool statusSwitch = true;// 控制物体是否继续上升的开关
    public GameObject parentBall;// 父物体（球）

    public GameObject musicObject;
    public bool isOnTrigger = false;// 判断是否触发了碰撞
	// Use this for initialization
    void Start () {
		parentBall = GameObject.Find ("center");// 找到父物体
        //line = this.GetComponent<LineRenderer> ();
        musicObject = GameObject.Find ("music");
    }
	
	// Update is called once per frame
	void Update () {
        if (instantiateball.fail) 
        {
            // 如果 statusSwitch 为 true，物体继续上升
            if (statusSwitch) {
                float UpLong = veloCity ;

                transform.position = new Vector3(transform.position.x, transform.position.y + UpLong, 0); // 物体沿 y 轴上升，速度由 veloCity 控制
                //Debug.Log(transform.position.y);
                //Debug.Log(transform.localPosition.y);


                if (transform.localPosition.y >= 100)
                {
                    transform.parent = parentBall.transform;//设置物体的父物体为 center（父球）
                    transform.SetAsFirstSibling();
                    statusSwitch = false;
                    instantiateball.fail = false;
                    instantiateball.score++;
                    instantiateball.Instance.IsNextLevel();
                    instantiateball.Instance.AddBalls();
                }

            }
        }
        
	}
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("ball"))
        {
            instantiateball.Instance.SetShade(true);
            isOnTrigger = true;
            parentBall.GetComponent<ballTurn>().onStop();
            musicObject.GetComponent<music_scripts>().onMusic2Play();
            Invoke("OnOver", 0.5f);
        }
    }

    void OnOver(){
        instantiateball.Instance.SetShade(false);
        instantiateball.score = 0;
        LevelController.Instance.UpdateLevel(1);
        parentBall.GetComponent<ballTurn>().onPlay();
    }
}
